home *** CD-ROM | disk | FTP | other *** search
/ Future Workshop / Future Workshop.iso / multimed / qtw111 / samples / mathcomp.cpp < prev    next >
C/C++ Source or Header  |  1993-03-16  |  19KB  |  545 lines

  1. // ---------------------------------------------------------------------
  2. //
  3. // MathComp.cpp - Math Components - QuickTime for Windows
  4. //
  5. //                Version 1.0
  6. //
  7. //                (c) 1988-1993 Apple Computer, Inc. All Rights Reserved.
  8. //
  9. // ---------------------------------------------------------------------
  10.  
  11.  
  12. // Includes
  13. // --------
  14. #include <windows.h>
  15. #include <windowsx.h>
  16. #include <qtw.h>
  17. #include <compmgr.h>
  18. #include "mathcomp.h"
  19.  
  20.  
  21. // No name decoration from C++
  22. // ---------------------------
  23. #ifdef __cplusplus
  24. extern "C" {
  25. #endif
  26.  
  27.  
  28. // Function Selectors
  29. // ------------------
  30.  
  31. // Add Component
  32. #define fsAdd          0
  33.  
  34. // Sum Component
  35. #define fsSum          0
  36. #define fsSetSum       1
  37. #define fsGetGlobalSum 2
  38. #define fsSetGlobalSum 3
  39.  
  40.  
  41. // Definitions
  42. // -----------
  43. #define ADD_VERSION    0x01000000
  44. #define SUM_VERSION    0x01000000
  45.  
  46.  
  47. // Typedefs
  48. // --------
  49. typedef struct tagSHARED {
  50.    short sTotal;
  51. } SHARED, FAR *LPSHARED;
  52.  
  53. typedef struct tagPRIVATE {
  54.    short sTotal;
  55.    LPSHARED lpShared;
  56. } PRIVATE, FAR *LPPRIVATE;
  57.  
  58.  
  59.  
  60. ////////////////////////////////////////////////////////////////////////////
  61. // Component Manager Interface Section
  62. //
  63.  
  64. // Glue code pointers from QTW.LIB
  65. // -------------------------------
  66. extern ENTRYFUNC lpfnQTToolbox;
  67. extern ENTRYFUNC lpfnQTComponentManager;
  68.  
  69.  
  70. // Internal Function Declarations
  71. // ------------------------------
  72. VOID FAR PASCAL FixupTB (ENTRYFUNC lpfnTB);
  73. VOID FAR PASCAL FixupCM (ENTRYFUNC lpfnCM);
  74.  
  75. // Prototypes of ASM entry point functions
  76. // ---------------------------------------
  77. DWORD FAR CDECL AddEntryPoint (VOID);
  78. DWORD FAR CDECL SumEntryPoint (VOID);
  79.  
  80.  
  81. // Data structure defining components in this DLL
  82. // ----------------------------------------------
  83. ComponentDescription cdTable [2] =   // Number of components
  84. {
  85. {
  86.    QTFOURCC ('a','d','d','2'),       // ostypeComponentType
  87.    QTFOURCC ('s','h','r','t'),       // ostypeComponentSubType
  88.    QTFOURCC ('a','p','p','l'),       // ostypeComponentManufacturer
  89.    0,                                // dwComponentFlags
  90.    0,                                // dwComponentFlagsMask
  91.    (ComponentRoutine) AddEntryPoint, // crEntryPoint
  92.    0,                                // hrsrcName
  93.    0,                                // hrsrcInfo
  94.    0                                 // hrsrcIcon
  95. },
  96. {
  97.    QTFOURCC ('s','u','m',' '),       // ostypeComponentType
  98.    QTFOURCC ('s','h','r','t'),       // ostypeComponentSubType
  99.    QTFOURCC ('a','p','p','l'),       // ostypeComponentManufacturer
  100.    0,                                // dwComponentFlags
  101.    0,                                // dwComponentFlagsMask
  102.    (ComponentRoutine) SumEntryPoint, // crEntryPoint
  103.    0,                                // hrsrcName
  104.    0,                                // hrsrcInfo
  105.    0                                 // hrsrcIcon
  106. }
  107. };
  108.  
  109.  
  110. // Function: THNGIDENTIFY - Required routine for components
  111. // --------------------------------------------------------------------
  112. // Parameters: LPCID FAR *lplpcid         pointer to LPCID
  113. //
  114. // Returns:    'thng' constant
  115. // --------------------------------------------------------------------
  116. OSType FAR PASCAL THNGIDENTIFY (LPCID FAR *lplpcid) {
  117.  
  118.     HGLOBAL hmem;
  119.     LPCID   lpcid;
  120.  
  121.     // Allocate memory to store CID info (will be freed by Component Manager)
  122.     if ((hmem = GlobalAlloc (GHND, sizeof CID)) == NULL)
  123.         return 0;
  124.     if ((lpcid = (LPCID) GlobalLock (hmem)) == NULL) {
  125.         GlobalFree (hmem);
  126.         return 0;
  127.     }
  128.  
  129.     // Fill table
  130.     lpcid->lVersion = CID_VERSION;      // Version of CompMgr interface
  131.     lpcid->sComponentCount = 2;         // Number of components this DLL
  132.     lpcid->lpcdTable = (LPCD) &cdTable; // Component Table
  133.     lpcid->lpfnTBFixup = FixupTB;       // Quick fixup routine for Toolbox
  134.     lpcid->lpfnCMFixup = FixupCM;       // Quick fixup routine for CompMgr
  135.     *lplpcid = lpcid;                   // Store the pointer
  136.  
  137.     return THING;                       // 'thng' identifies component
  138. }
  139.  
  140.  
  141. // Function: FixupTB - Set glue ToolBox pointer to given value
  142. // --------------------------------------------------------------------
  143. // Parameters: ENTRYFUNC lpfnTB           pointer to ToolBox entry point
  144. //
  145. // Returns:    VOID
  146. // --------------------------------------------------------------------
  147. VOID FAR PASCAL FixupTB (ENTRYFUNC lpfnTB) {
  148.  
  149.     lpfnQTToolbox = lpfnTB;
  150.  
  151.     return;
  152. }
  153.  
  154.  
  155. // Function: FixupCM - Set glue Component Manager pointer to given value
  156. // --------------------------------------------------------------------
  157. // Parameters: ENTRYFUNC lpfnCM           pointer to CompMgr entry point
  158. //
  159. // Returns:    VOID
  160. // --------------------------------------------------------------------
  161. VOID FAR PASCAL FixupCM (ENTRYFUNC lpfnCM) {
  162.  
  163.     lpfnQTComponentManager = lpfnCM;
  164.  
  165.     return;
  166. }
  167.  
  168.  
  169. ////////////////////////////////////////////////////////////////////////////
  170. ////////////////////////////////////////////////////////////////////////////
  171. //  COMPONENT:   add2  shrt  -  Add two 16-bit (short) values
  172. //
  173.  
  174. // Function: cfAddOpenSelect - Open an add component instance
  175. // --------------------------------------------------------------------
  176. // Parameters: STKOFF_CMP so;             component stack offset to parms
  177. //             ComponentInstance ci;      component instance handle
  178. //
  179. // Returns:    ComponentResult
  180. // --------------------------------------------------------------------
  181. ComponentResult QTAPI cfAddOpenSelect (STKOFF_CMP so, ComponentInstance ci) {
  182.  
  183.     return noErr;
  184. }
  185.  
  186.  
  187. // Function: cfAddCloseSelect - Close an open component instance
  188. // --------------------------------------------------------------------
  189. // Parameters: STKOFF_CMP so;             component stack offset to parms
  190. //             ComponentInstance ci;      component instance handle
  191. //
  192. // Returns:    ComponentResult
  193. // --------------------------------------------------------------------
  194. ComponentResult QTAPI cfAddCloseSelect (STKOFF_CMP so, ComponentInstance ci) {
  195.  
  196.     return noErr;
  197. }
  198.  
  199.  
  200. // Function: cfAddCanDoSelect - Report if function is implemented
  201. // --------------------------------------------------------------------
  202. // Parameters: STKOFF_CMP so;             component stack offset to parms
  203. //             LONG lFunctionSelector;    function selector
  204. //
  205. // Returns:    ComponentResult
  206. // --------------------------------------------------------------------
  207. ComponentResult QTAPI cfAddCanDoSelect (STKOFF_CMP so, LONG lFunctionSelector) {
  208.  
  209.     switch (lFunctionSelector) {
  210.         case kComponentOpenSelect:
  211.         case kComponentCloseSelect:
  212.         case kComponentCanDoSelect:
  213.         case kComponentVersionSelect:
  214.         case kComponentRegisterSelect:
  215.         case kComponentTargetSelect:
  216.         case fsAdd:
  217.             return TRUE;
  218.         default:
  219.             return FALSE;
  220.     }
  221. }
  222.  
  223.  
  224. // Function: cfAddVersionSelect - Report component version
  225. // --------------------------------------------------------------------
  226. // Parameters: STKOFF_CMP so;             component stack offset to parms
  227. //             ComponentInstance ci;      component instance handle
  228. //
  229. // Returns:    ComponentResult
  230. // --------------------------------------------------------------------
  231. ComponentResult QTAPI cfAddVersionSelect (STKOFF_CMP so, ComponentInstance ci) {
  232.  
  233.     return ADD_VERSION;
  234. }
  235.  
  236.  
  237. // Function: cfAddRegisterSelect - Report if able to register
  238. // --------------------------------------------------------------------
  239. // Parameters: STKOFF_CMP so;             component stack offset to parms
  240. //             ComponentInstance ci;      component instance handle
  241. //
  242. // Returns:    ComponentResult
  243. // --------------------------------------------------------------------
  244. ComponentResult QTAPI cfAddRegisterSelect (STKOFF_CMP so, ComponentInstance ci) {
  245.  
  246.     return 0;
  247. }
  248.  
  249.  
  250. // Function: cfAddTargetSelect - Not implemented
  251. // --------------------------------------------------------------------
  252. // Parameters: STKOFF_CMP so;             component stack offset to parms
  253. //             ComponentInstance ci;      component instance handle
  254. //
  255. // Returns:    ComponentResult
  256. // --------------------------------------------------------------------
  257. ComponentResult QTAPI cfAddTargetSelect (STKOFF_CMP so, ComponentInstance ci) {
  258.  
  259.     return 0;
  260. }
  261.  
  262.  
  263. ////////////////////////////////////////////////////////////////////////////
  264. // Add2 Functions
  265. //
  266.  
  267. // Function: cfAdd2 - Add two short values
  268. // --------------------------------------------------------------------
  269. // Parameters: STKOFF_CMP so;             component stack offset to parms
  270. //             LPVOID lpvStorage;         pointer to instance storage
  271. //             short sOne;                first value to add
  272. //             short wTwo;                second value to add
  273. //
  274. // Returns:    short                      sum of two given values
  275. // --------------------------------------------------------------------
  276. short QTAPI cfAdd2 (STKOFF_CMP so, LPVOID lpvStorage, short sOne, short sTwo) {
  277.  
  278.     return sOne + sTwo;
  279. }
  280.  
  281.  
  282.  
  283. ////////////////////////////////////////////////////////////////////////////
  284. ////////////////////////////////////////////////////////////////////////////
  285. //  COMPONENT:   sum   shrt  -  Sum 16-bit (short) values
  286. //
  287.  
  288. // Function: cfSumOpenSelect - Open a sum component instance
  289. // --------------------------------------------------------------------
  290. // Parameters: STKOFF_CMP so;             component stack offset to parms
  291. //             ComponentInstance ci;      component instance handle
  292. //
  293. // Returns:    ComponentResult
  294. // --------------------------------------------------------------------
  295. ComponentResult QTAPI cfSumOpenSelect (STKOFF_CMP so, ComponentInstance ci) {
  296.  
  297.     // Allocate private storage for this instance
  298.     LPPRIVATE lpPrivate;
  299.     if ((lpPrivate = (LPPRIVATE) GlobalAllocPtr (GHND, sizeof LPPRIVATE)) == NULL )
  300.         return insufficientMemory;
  301.  
  302.     // Inform Component Manager of allocated storage for this instance
  303.     SetComponentInstanceStorage (ci, (LPVOID) lpPrivate);
  304.  
  305.     // If this is the first instance allocate shared memory
  306.     LPSHARED lpShared;
  307.     if ((lpShared = (LPSHARED) GetComponentRefcon ((Component) ci)) == NULL) {
  308.         if ((lpShared = (LPSHARED) GlobalAllocPtr (GHND, sizeof LPSHARED)) == NULL) {
  309.             GlobalFreePtr (lpPrivate);
  310.             return insufficientMemory;
  311.         }
  312.         SetComponentRefcon ((Component) ci, (LONG) lpShared);
  313.     }
  314.  
  315.     // Provide quick access to the shared memory
  316.     lpPrivate->lpShared = lpShared;
  317.  
  318.     return noErr;
  319. }
  320.  
  321.  
  322. // Function: cfSumCloseSelect - Close an open component instance
  323. // --------------------------------------------------------------------
  324. // Parameters: STKOFF_CMP so;             component stack offset to parms
  325. //             ComponentInstance ci;      component instance handle
  326. //
  327. // Returns:    ComponentResult
  328. // --------------------------------------------------------------------
  329. ComponentResult QTAPI cfSumCloseSelect (STKOFF_CMP so, ComponentInstance ci) {
  330.  
  331.     // Get the private memory pointer
  332.     LPPRIVATE lpPrivate;
  333.     lpPrivate = (LPPRIVATE) GetComponentInstanceStorage (ci);
  334.  
  335.     // Free shared memory if closing last instance
  336.     if (CountComponentInstances ((Component) ci) == 1) {
  337.         if (lpPrivate)
  338.             GlobalFreePtr (lpPrivate->lpShared);
  339.  
  340.         // Prepare for next re-open
  341.         SetComponentRefcon ((Component) ci, 0);
  342.     }
  343.  
  344.     // Free private instance memory
  345.     GlobalFreePtr (lpPrivate);
  346.  
  347.     return noErr;
  348. }
  349.  
  350.  
  351. // Function: cfSumCanDoSelect - Report if function is implemented
  352. // --------------------------------------------------------------------
  353. // Parameters: STKOFF_CMP so;             component stack offset to parms
  354. //             LONG lFunctionSelector;    function selector
  355. //
  356. // Returns:    ComponentResult
  357. // --------------------------------------------------------------------
  358. ComponentResult QTAPI cfSumCanDoSelect (STKOFF_CMP so, LONG lFunctionSelector) {
  359.  
  360.     switch (lFunctionSelector) {
  361.         case kComponentOpenSelect:
  362.         case kComponentCloseSelect:
  363.         case kComponentCanDoSelect:
  364.         case kComponentVersionSelect:
  365.         case kComponentRegisterSelect:
  366.         case kComponentTargetSelect:
  367.         case fsSum:
  368.         case fsSetSum:
  369.         case fsGetGlobalSum:
  370.         case fsSetGlobalSum:
  371.             return TRUE;
  372.         default:
  373.             return FALSE;
  374.     }
  375. }
  376.  
  377.  
  378. // Function: cfSumVersionSelect - Report component version
  379. // --------------------------------------------------------------------
  380. // Parameters: STKOFF_CMP so;             component stack offset to parms
  381. //             ComponentInstance ci;      component instance handle
  382. //
  383. // Returns:    ComponentResult
  384. // --------------------------------------------------------------------
  385. ComponentResult QTAPI cfSumVersionSelect (STKOFF_CMP so, ComponentInstance ci) {
  386.  
  387.     return SUM_VERSION;
  388. }
  389.  
  390.  
  391. // Function: cfSumRegisterSelect - Report if able to register
  392. // --------------------------------------------------------------------
  393. // Parameters: STKOFF_CMP so;             component stack offset to parms
  394. //             ComponentInstance ci;      component instance handle
  395. //
  396. // Returns:    ComponentResult
  397. // --------------------------------------------------------------------
  398. ComponentResult QTAPI cfSumRegisterSelect (STKOFF_CMP so, ComponentInstance ci) {
  399.  
  400.     return 0;
  401. }
  402.  
  403.  
  404. // Function: cfSumTargetSelect - Not implemented
  405. // --------------------------------------------------------------------
  406. // Parameters: STKOFF_CMP so;             component stack offset to parms
  407. //             ComponentInstance ci;      component instance handle
  408. //
  409. // Returns:    ComponentResult
  410. // --------------------------------------------------------------------
  411. ComponentResult QTAPI cfSumTargetSelect (STKOFF_CMP so, ComponentInstance ci) {
  412.  
  413.     return 0;
  414. }
  415.  
  416.  
  417. ////////////////////////////////////////////////////////////////////////////
  418. // Sum Functions
  419. //
  420.  
  421. // Function: cfSum - Add given value to running total
  422. // --------------------------------------------------------------------
  423. // Parameters: STKOFF_CMP so;             component stack offset to parms
  424. //             LPVOID lpvStorage;         pointer to instance storage
  425. //             short sOne;                first short to add
  426. //             short wTwo;                second short to add
  427. //
  428. // Returns:    short                      instance total
  429. // --------------------------------------------------------------------
  430. short QTAPI cfSum (STKOFF_CMP so, LPVOID lpvStorage, short sValue) {
  431.  
  432.     LPPRIVATE lpPrivate = (LPPRIVATE) lpvStorage;
  433.  
  434.     // Add the given value into instance total
  435.     lpPrivate->sTotal += sValue;
  436.  
  437.     // Add the given value into global total
  438.     lpPrivate->lpShared->sTotal += sValue;
  439.  
  440.     // Return the instance total
  441.     return lpPrivate->sTotal;
  442. }
  443.  
  444.  
  445. // Function: cfSetSum - Set instance total to given value
  446. // --------------------------------------------------------------------
  447. // Parameters: STKOFF_CMP so;             component stack offset to parms
  448. //             LPVOID lpvStorage;         pointer to instance storage
  449. //             short sValue;              value to set
  450. //
  451. // Returns:    short                      instance total after set
  452. // --------------------------------------------------------------------
  453. short QTAPI cfSetSum (STKOFF_CMP so, LPVOID lpvStorage, short sValue) {
  454.  
  455.     LPPRIVATE lpPrivate = (LPPRIVATE) lpvStorage;
  456.  
  457.     // Set the given value into instance total
  458.     lpPrivate->sTotal = sValue;
  459.  
  460.     // Return the instance total
  461.     return lpPrivate->sTotal;
  462. }
  463.  
  464.  
  465. // Function: cfSetGlobalSum - Set global total to given value
  466. // --------------------------------------------------------------------
  467. // Parameters: STKOFF_CMP so;             component stack offset to parms
  468. //             LPVOID lpvStorage;         pointer to instance storage
  469. //             short sValue;              value to set
  470. //
  471. // Returns:    short                      global total after set
  472. // --------------------------------------------------------------------
  473. short QTAPI cfSetGlobalSum (STKOFF_CMP so, LPVOID lpvStorage, short sValue) {
  474.  
  475.     LPPRIVATE lpPrivate = (LPPRIVATE) lpvStorage;
  476.  
  477.     // Set the given value into global total
  478.     lpPrivate->lpShared->sTotal = sValue;
  479.  
  480.     // Return the global total
  481.     return lpPrivate->lpShared->sTotal;
  482. }
  483.  
  484.  
  485. // Function: cfGetGlobalSum - Return the global total
  486. // --------------------------------------------------------------------
  487. // Parameters: STKOFF_CMP so;             component stack offset to parms
  488. //             LPVOID lpvStorage;         pointer to instance storage
  489. //
  490. // Returns:    short                      global total
  491. // --------------------------------------------------------------------
  492. short QTAPI cfGetGlobalSum (STKOFF_CMP so, LPVOID lpvStorage) {
  493.  
  494.     LPPRIVATE lpPrivate = (LPPRIVATE) lpvStorage;
  495.  
  496.     // Return the global total
  497.     return lpPrivate->lpShared->sTotal;
  498. }
  499.  
  500.  
  501. #ifdef __cplusplus
  502.   } // extern "C"
  503. #endif
  504.  
  505.  
  506.  
  507. ////////////////////////////////////////////////////////////////////////////
  508. // Standard Windows DLL interface functions
  509. //
  510.  
  511. // Function: LibMain - DLL initialization routine called by Windows
  512. // --------------------------------------------------------------------
  513. // Parameters: HANDLE hInstance;          DLL instance handle
  514. //             WORD   wDataSegment;       Value of DS register
  515. //             WORD   wHeapSize;          Heap size
  516. //             LPSTR  lpszCmdLine;        Command line info
  517. //
  518. // Returns:    TRUE if initialization successful; FALSE otherwise
  519. // --------------------------------------------------------------------
  520. int FAR PASCAL LibMain (HINSTANCE hInstance, WORD wDataSegment,
  521.                         WORD wHeapSize, LPSTR lpszCmdLine) {
  522.  
  523.     // Unlock the current movable data segment
  524.     if (wHeapSize > 0)
  525.         UnlockData (0);
  526.  
  527.     // Return to Windows
  528.     return TRUE;
  529. }
  530.  
  531.  
  532. // Function: WEP - Windows exit procedure
  533. // --------------------------------------------------------------------
  534. // Parameters: int nExitType              Exit type
  535. //
  536. // Returns:    Always 1
  537. // --------------------------------------------------------------------
  538. extern "C" BOOL FAR PASCAL WEP (int nValue);
  539. #pragma alloc_text(WEP_TEXT,WEP)
  540. extern "C" BOOL FAR PASCAL WEP (int nValue) {
  541.  
  542.     return TRUE;
  543. }
  544.  
  545.